package com.dk.util.http.interceptor;

import com.dk.enumerate.HttpEnum;
import com.dk.util.config.ReadConfig;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

/**
 * okttp默认头部拦截器
 */
public class HeaderInterceptor implements Interceptor {
    /**
     * 新增公共请求头拦截器
     * @param chain 拦截器链式对象
     * @return 链式对象
     * @throws IOException
     */
    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        //获取请求对象
        Request request = chain.request();
        //重新构建请求对象(增加公共请求头)
        request = request.newBuilder()
                .addHeader(HttpEnum.HEADER_CONNECTION.getValue(), ReadConfig.CONNECTION)
                .addHeader(HttpEnum.HEADER_CONTENTTYPE.getValue(), ReadConfig.CONTENTTYPE)
                .build();
        return chain.proceed(request);
    }
}
